﻿using SalesSys.WebCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Mvc;
using Aspose.Words;
using Aspose.Words.Saving;
using SalesSys.BLL.Sales;

namespace SalesSys.Site.Controllers
{
    /// <summary>
    /// 文件打印
    /// </summary>
    public class PrintController : BaseController
    {
        // GET: Print
        public ActionResult Index()
        {
            return View();
        }

       /// <summary>
       /// 打印运单
       /// </summary>
       /// <param name="orderId">计划单编号</param>
       /// <returns></returns>
        public ActionResult DeliveryOrder(Guid orderId)
        {
            MemoryStream sm = new MemoryStream();
            ViewData.Model = "";
            var html = RenderPartial("");

            return File(sm, "application/octet-stream");

        }

        /// <summary>
        /// 打印物联合同
        /// </summary>
        /// <param name="orderId">计划单编号</param>
        public void SalesContract(Guid orderId)
        {
            OrderBLL bll = new OrderBLL();
            ViewData.Model = bll.Details(orderId);
            ToPdfOpen("SalesContract");
        }

        /// <summary>
        /// 打印沐威合同
        /// </summary>
        /// <param name="orderId">计划单编号</param>
        public void SalesContractMW(Guid orderId)
        {
            OrderBLL bll = new OrderBLL();
            ViewData.Model = bll.Details(orderId);
            ToPdfOpen("SalesContractMW");
        }

        /// <summary>
        /// 打印提货单
        /// </summary>
        /// <param name="orderId">计划单编号</param>
        public void StockOut(Guid orderId)
        {
            OrderBLL bll = new OrderBLL();
            ViewData.Model = bll.Details(orderId);
            ToPdfOpen("StockOut");
        }

        private void ToPdfOpen(string viewName)
        {
            MemoryStream sm = new MemoryStream();
            var wr = new StreamWriter(sm);
            var str = string.Format("~/Views/Print/{0}.cshtml", viewName);
            RenderPartial(str, wr);
            sm.Position = 0;
            Document d = new Document(sm);
            d.Sections[0].PageSetup.RightMargin = 50;
            d.Sections[0].PageSetup.LeftMargin = 50;
            d.Save(System.Web.HttpContext.Current.Response, "show.pdf", ContentDisposition.Inline,
                SaveOptions.CreateSaveOptions(SaveFormat.Pdf));

        }

        private HttpResponseMessage ToPdfOpenApi(string viewName)
        {
            MemoryStream sm = new MemoryStream();
            var wr = new StreamWriter(sm);
            RenderPartial(viewName, wr);
            sm.Position = 0;
            Document d = new Document(sm);
            sm = new MemoryStream();
            d.Save(sm, SaveFormat.Pdf);
            HttpResponseMessage response = new HttpResponseMessage();
            response.Content = new StreamContent(sm);

            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("Inline");
            response.Content.Headers.ContentDisposition.FileName = "show.pdf";
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

            return response;
        }
    }
}